package com.wgspring.controller;

import com.wgspring.model.Photograph;
import com.wgspring.service.PhotographService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.*;

@Controller
public class FileController {

    @Autowired
    private PhotographService photographService;


    @RequestMapping("upload")
    public String upload() {
        return "upload";
    }

    @RequestMapping("upload-commit")
    @ResponseBody
    public void uploadCommit(MultipartHttpServletRequest request) throws IOException {

        Iterator<String> fileNames = request.getFileNames();

        while (fileNames.hasNext()) {
            String fileName = fileNames.next();
            MultipartFile file = request.getFile(fileName);
            photographService.addPhoto(file);
        }
    }

    //获取lastTime之前的一次照片集合
    @RequestMapping("getPhotographs")
    @ResponseBody
    public List<Photograph> getPhotographs(Long lastTime) {
        return photographService.getOnedayPhotographsBeforeTime(lastTime);
    }

    @RequestMapping("details/{time}")
    public String detailsPage(@PathVariable() Long time, Model model) {
        List<Photograph> list = getPhotographs(time);
        model.addAttribute("files",list);

        return "details";
    }

}
